package com.loves.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.loves.admin.mapper.ProductCategoryMapper;
import com.loves.admin.mapper.ProductMapper;
import com.loves.admin.model.Product;
import com.loves.admin.model.ProductCategory;
import com.loves.admin.service.ProductCategoryService;
import com.loves.admin.util.common.Constant;
import com.loves.admin.util.exception.BizException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {

    @Resource
    private ProductCategoryMapper productCategoryMapper;

    @Resource
    private ProductMapper productMapper;

    @Override
    public IPage<ProductCategory> selectByParent(Page<ProductCategory> page) {
        return productCategoryMapper.selectByParent(page);
    }

    @Override
    public IPage<ProductCategory> selectByItem(Page<ProductCategory> page, String id) {
        return productCategoryMapper.selectByItem(page,id
        );
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int create(ProductCategory productCategory) {
        setCategoryLevel(productCategory);
        productCategory.setCreateDate(LocalDateTime.now());
        productCategory.setCreateName("系统");
        return productCategoryMapper.insert(productCategory);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(ProductCategory productCategory) {
        setCategoryLevel(productCategory);
        return productCategoryMapper.updateById(productCategory);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delete(String id) {
        ProductCategory productCategory = productCategoryMapper.selectById(id);
        //如果是父节点
        if(productCategory.getParentId().equals(Constant.PARENT_ID)){
            QueryWrapper<ProductCategory> categoryQueryWrapper = new QueryWrapper<>();
            categoryQueryWrapper.lambda().eq(ProductCategory::getParentId,productCategory.getParentId()).eq(ProductCategory::getDeleteFlag,false);
            List<ProductCategory> productCategories = productCategoryMapper.selectList(categoryQueryWrapper);
            if(productCategories != null){
                productCategories.forEach(category ->{
                    productCount(category.getId());
                    //如果没关联商品，则删除子节点信息
                    category.setDeleteFlag(true);
                    productCategoryMapper.updateById(category);
                });
            }
        }else{
            productCount(id);
        }
        productCategory.setDeleteFlag(true);
        return productCategoryMapper.updateById(productCategory);
    }

    /**
     * 根据categoryId查看是否关联商品信息
     * @param categoryId
     */
    private void productCount(String categoryId){
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Product::getProductCategoryId,categoryId).eq(Product::getDeleteFlag,false);
        int productCount = productMapper.selectCount(queryWrapper);
        if(productCount > 0){
            throw new BizException("商品分类已关联商品信息，不可删除！");
        }
    }

    /**
     * 设置level
     */
    private void setCategoryLevel(ProductCategory productCategory){
        //没有父分类时为一级分类
        if(productCategory.getParentId().equals(Constant.PARENT_ID)){
            productCategory.setLevel(Constant.LEVEL);
        }else {
            QueryWrapper<ProductCategory> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(ProductCategory::getParentId,productCategory.getId());
            ProductCategory parentCategory = productCategoryMapper.selectOne(queryWrapper);
            if(parentCategory != null){
                productCategory.setLevel(parentCategory.getLevel()+1);
            }else {
                productCategory.setLevel(Constant.LEVEL);
            }
        }
    }
}
